package com.ali.juejin.model.login

import android.util.Log
import android.view.View
import android.widget.Toast
import androidx.databinding.ObservableField
import com.ali.juejin.base.BaseModelView
import com.ali.juejin.bean.LoginBean
import com.ali.juejin.config.UrlConfig
import com.ali.juejin.services.LoginServices
import com.ali.juejin.utils.Request
import com.blankj.utilcode.util.DeviceUtils
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.RegexUtils

class LoginModel: BaseModelView() {

    var user = ObservableField<String>()
    var psd = ObservableField<String>()

    val net by lazy { Request.instance(UrlConfig.AUTHCENTER) }

    // 使用手机登录登录
    fun login(view:View){

        if(user.get().isNullOrBlank()){
            Log.w("err","用户名未填写")
            Toast.makeText(view.context,"用户名未填写",Toast.LENGTH_SHORT).show()
            return
        }
        if(psd.get().isNullOrBlank()){
            Log.w("err","密码未填写")
            Toast.makeText(view.context,"密码未填写",Toast.LENGTH_SHORT).show()
            return
        }
        Log.w("username",user.get()!!)
        Log.w("password",psd.get()!!)
        val username = user.get()!!
        val password = psd.get()!!
        val deviceId = DeviceUtils.getAndroidID()
        var loginType = "tel"
        if(RegexUtils.isEmail(username)){
            loginType = "email"
        }else if(RegexUtils.isTel(username)){
            loginType = "tel"
        }
        Log.w("loginType",loginType)
        request<LoginBean>(
            {
                val data = net.create(LoginServices::class.java).login(username,password,loginType,deviceId,deviceId)
                LogUtils.w("response",data.m)
            }
        )
    }

}